package com.example.controller;

import com.alibaba.fastjson2.JSONObject;
import com.example.domain.Company;
import com.example.domain.Result;
import com.example.domain.User;
import com.example.service.CompanyService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/company")
public class CompanyController extends BaseController {

    @Autowired
    private CompanyService companyService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public Result list(Company company) {
        PageInfo<Company> page = this.companyService.selectPageList(company);
        return success(page);
    }

    /**
     * 添加或修改
     */
    @PostMapping("/save")
    public Result save(Company company, HttpSession session) {
        User user = (User) session.getAttribute("login");
        this.companyService.save(company, user);
        return success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Long id) {
        Company company = this.companyService.selectById(id);
        return success(company);
    }

    @PostMapping("/del/{id}")
    public Result delById(@PathVariable("id") Long id) {
        this.companyService.deleteById(id);
        return success();
    }

    @GetMapping("/options")
    public Result options() {
        List<JSONObject> list = companyService.options();
        return success(list);
    }
}
